package com.runda.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import com.bbjob.dto.Result;
import com.bbjob.model.Production;
import com.runda.biz.service.IProductionService;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("production")
public class ProductionController {
    @Resource
    private IProductionService prodService;

    @GetMapping("list")
    public Result<List<Production>> getList(String typeCode,String query){
        List<Production> list= prodService.getList(typeCode,query);
        return new Result<List<Production>>(list);
    }
    @GetMapping("get/{id}")
    public Result<Production> getProduct(@PathVariable("id") Integer id){
        Production product=this.prodService.selectByKey(id);
        return new Result<Production>(product);
    }
    @PostMapping("save")
    public Result<Production> save(@RequestBody Production production){
        int ok=this.prodService.save(production);
        if(ok>0)
        return new Result<Production>(production);
        return new Result<Production>(5000,"未知原因保存失败,请联系管理员。");
    }
    @GetMapping("del/{productId}")
    public Result<String> del(@PathVariable("productId") Integer productId){
        int ok = this.prodService.delete(productId);
        if(ok>0)
        return new Result<String>();
        return Result.GLOBAL_ERROR();
    }
    @PostMapping("edit/{id}")
    public Result<String> editProduct(@PathVariable("id") Integer id, @RequestBody Production product){
        product.setId(id);
        int ok=this.prodService.updateNotNull(product);
        if(ok>0)
        return new Result<String>();
        return Result.GLOBAL_ERROR();
    }
}